package com.linyubing.servlet;

import com.alibaba.fastjson.JSON;
import com.linyubing.dao.BaseDao;
import com.linyubing.dao.BaseDaoImpl;
import com.linyubing.pojo.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(urlPatterns = "/student/*")
public class StudentServlet extends BaseServlet  {
    //创建BaseDaoImpl对象
    BaseDao<Student> dao = new BaseDaoImpl<>(Student.class);
    public void getAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //查询所有的学生
        List<Student> studentList = dao.getAll();
        if(studentList!=null&&studentList.size()!=0){
            //转换为json
            String jsonString = JSON.toJSONString(studentList);
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write(jsonString);
        }

    }
    public void getById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //获取请求参数
        int stuId= Integer.parseInt(req.getParameter("stuId"));
        //调用数据库的根据id查询数据
        Student student = dao.getById(stuId);
        if(student!=null){
            //转换为json
            String jsonString = JSON.toJSONString(student);
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write(jsonString);
        }
    }
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //post请求的乱码问题
        req.setCharacterEncoding("utf-8");
        //req.getParameter:不能获取请求体的数据
        //获取请求参数
        BufferedReader reader = req.getReader();
        String params = reader.readLine();
        Student student = JSON.parseObject(params, Student.class);
        boolean flag = dao.add(student);
        if(flag){
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write("success");
        }
    }
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, InvocationTargetException, IllegalAccessException {
        //post请求的乱码问题
        req.setCharacterEncoding("utf-8");
        //获取请求参数（post方法）
        BufferedReader reader = req.getReader();
        String params = reader.readLine();
        Student student = JSON.parseObject(params, Student.class);
        //调用数据库的更新方法
        boolean flag = dao.update(student);
        if(flag){
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write("success");
        }
    }
    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        //获取请求参数
        int stuId= Integer.parseInt(req.getParameter("stuId"));
        //调用数据库的删除方法
        boolean flag = dao.deleteById(stuId);
        if(flag){
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write("success");
        }
    }
}
